﻿using System;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;
using YaAppoint.Appointments.Pools;

namespace YaAppoint.Appointments.ScheduleOverrides
{
    public class ScheduleOverrideManager(IRepository<ScheduleOverride,Guid > scheduleOverrideReppsitory,
                                         IRepository<Pool,Guid> poolRepository):DomainService
    {
        private readonly IRepository<ScheduleOverride, Guid> _scheduleOverrideReppsitory = scheduleOverrideReppsitory;
        private readonly IRepository<Pool,Guid> _poolRepository = poolRepository;

        public async Task Delete(ScheduleOverride scheduleOverride)
        {
            foreach (var pool in scheduleOverride.Pools)
            {
                if (pool.Appointments.Count > 0)
                {
                    throw new BusinessException(CustomErrorCodes.AppointmentExistInSchduleOverride);
                }
            }
            foreach (var pool in scheduleOverride.Pools)
            { 
                await _poolRepository.DeleteAsync(pool);
            }
            await _scheduleOverrideReppsitory.DeleteAsync(scheduleOverride);
        }
    }
}
